package io.github.junxworks.qt.modules.mm.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;

import io.github.junxworks.ep.core.Result;
import io.github.junxworks.ep.core.utils.PageUtils;
import io.github.junxworks.ep.sys.annotations.EpLog;
import io.github.junxworks.ep.sys.constants.RecordStatus;
import io.github.junxworks.junx.core.util.StringUtils;
import io.github.junxworks.qt.modules.mm.dto.MmExpressionCondition;
import io.github.junxworks.qt.modules.mm.dto.MmExpressionDto;
import io.github.junxworks.qt.modules.mm.service.MmExpressionService;

/**
 * mm_expression Entity Controller Class
 *
 * @since 2022-3-22 14:05:54 Generated by JunxEP
 */
@RestController
@RequestMapping("/mm/expressions")
public class MmExpressionController {
	@Autowired
	private MmExpressionService mmExpressionService;

	/**
	 * Query list.
	 *
	 * @param condition the condition
	 * @return the result
	 */
	@GetMapping()
	public Result queryMmExpressionList(MmExpressionCondition condition) {
		//查询列表数据
		PageUtils.setPage(condition);
		return Result.ok(new PageInfo<>(mmExpressionService.queryList(condition)));
	}

	/**
	 * Save .
	 *
	 * @param dto the dto
	 * @return the result
	 * @throws Exception 
	 */
	@EpLog("模型-表达式-保存表达式")
	@PostMapping()
	public Result saveMmExpression(@RequestBody MmExpressionDto dto) throws Exception {
		mmExpressionService.save(dto);
		return Result.ok();
	}

	/**
	 * Query entity by id.
	 *
	 * @param id the id
	 * @return the result
	 */
	@GetMapping("/{id}")
	public Result queryMmExpressionEntityById(@PathVariable("id") Long id) {
		return Result.ok(mmExpressionService.queryMmExpressionById(id));
	}

	@PostMapping("/name")
	public Result queryMmExpressionEntityByname(@RequestBody MmExpressionCondition dto) {
		return Result.ok(mmExpressionService.queryMmExpressionByName(dto.getExpName()));
	}

	/**
	 * Delete.
	 *
	 * @param id the id
	 * @return the result
	 * @throws Exception 
	 */
	@EpLog("模型-表达式-删除表达式")
	@DeleteMapping("/{id}")
	public Result deleteMmExpression(@PathVariable("id") Long id) throws Exception {
		String msg = mmExpressionService.checkExpCanDelete(id);
		if (StringUtils.notNull(msg)) {
			return Result.error(msg);
		}
		MmExpressionDto dto = new MmExpressionDto();
		dto.setId(id);
		dto.setStatus(RecordStatus.DELETED.getValue());
		mmExpressionService.save(dto);
		return Result.ok();
	}
}